package com.developmentReformProject.controller;

import com.developmentReformProject.annotation.MyLog;
import com.developmentReformProject.dao.*;
import com.developmentReformProject.dto.AdminQueryDto;
import com.developmentReformProject.dto.ApplyQueryDto;
import com.developmentReformProject.dto.EnterpriseQueryDto;
import com.developmentReformProject.dto.LogQueryDto;
import com.developmentReformProject.entity.*;
import com.developmentReformProject.service.*;
import com.developmentReformProject.utils.PageUtil;
import com.developmentReformProject.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping(value = "/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private SysLogDao sysLogDao;
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private ApplyDao applyDao;
    @Autowired
    private AuditProjectLibraryService auditProjectLibraryService;
    @Autowired
    private ThreeYearProjectLibraryService threeYearProjectLibraryService;
    @Autowired
    private FiveYearProjectLibraryService fiveYearProjectLibraryService;
    @Autowired
    private FiveProjectDao fiveProjectDao;
    @Autowired
    private ThreeProjectDao threeProjectDao;
    @Autowired
    HttpServletRequest request;
   /** @PostMapping("/loginLogin")
    public String login( String username, String password) throws Exception{
        //调用业务
        String str=null;
        String mag=adminService.loginAdmin(username,password);
        if(mag!=null){
            str="index";
        }

        return str;
    }*/
    @MyLog(value = "批量删除管理员")
    @PostMapping("/deleteBatch")
    public ResultVo deleteBatch(int[] adminNos) throws Exception{
        //调用业务
        adminService.deleteAdminBatch(adminNos);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "删除管理员")
    @PostMapping("/delete")
    public ResultVo delete(Integer adminNo) throws Exception{
        //调用业务
        adminService.deleteAdmin(adminNo);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "添加管理员")
    @PostMapping("/adminAdd")
    public ResultVo adminAdd(@RequestBody Admin admin) throws Exception{
        //调用业务
        adminService.addAdmin(admin);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "管理员修改信息")
    @PostMapping("/adminUpdate")
    public ResultVo adminUpdate(Integer adminNo,Admin admin)throws Exception{
        adminService.updateAdmin(adminNo,admin);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "通过管理员编号查找管理员")
    @GetMapping("/queryAdminById")
    public ResultVo queryAdminById(Integer adminNo)throws Exception{
        AdminQueryDto adminQueryDto=new AdminQueryDto();
        adminQueryDto.setAdminNo(adminNo);
        PageUtil<Admin> data=adminService.pageList(adminQueryDto);
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @MyLog(value = "管理员修改密码")
    @PostMapping("/updatePassword")
    public ResultVo updatePassword(String adminAccountNumber, String oldPassword, String newPassword) throws Exception {
        adminService.updatePasswordAdmin(adminAccountNumber,newPassword,oldPassword);
        ResultVo resultVo = new ResultVo();
        return resultVo;

    }
    @MyLog(value = "查询所有管理员")
    @GetMapping("/queryAllStateAdmin")
    public ResultVo queryAllStateAdmin()throws Exception{
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "管理员删除企业")
    @PostMapping("/deleteEnterpriseAdmin")
    public ResultVo deleteEnterpriseAdmin(int enterpriseId)throws Exception{
        adminService.deleteEnterpriseAdmin(enterpriseId);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value = "管理员修改企业信息")
    @PostMapping("/updateEnterpriseAdmin")
    public ResultVo updateEnterpriseAdmin(Enterprise enterprise)throws Exception{
        adminService.updateEnterpriseAdmin(enterprise);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @MyLog(value="管理员批量删除企业")
    @PostMapping("/deleteEnterpriseAdminBatch")
    public ResultVo deleteEnterpriseAdminBatch(int[] enterpriseIds)throws  Exception{
        adminService.deleteEnterpriseBatchAdmin(enterpriseIds);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @GetMapping("/queryList")
    public ResultVo list(AdminQueryDto adminQueryDto) throws Exception{

        //调用业务
        PageUtil<Admin> data=adminService.pageList(adminQueryDto);
        //响应
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @GetMapping("/queryEnterpriseList")
    public ResultVo listEnterprise(EnterpriseQueryDto enterpriseQueryDto) throws Exception{
        //调用业务
        PageUtil<Enterprise> data=enterpriseService.pageList(enterpriseQueryDto);
        //响应
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @MyLog(value = "根据权限查找管理员")
    @GetMapping("/queryAdminByPower")
    public ResultVo queryAdminByPower(int adminPower)throws Exception{
        AdminQueryDto adminQueryDto=new AdminQueryDto();
        adminQueryDto.setAdminPower(adminPower);
        PageUtil<Admin> data=adminService.pageList(adminQueryDto);
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @MyLog(value = "登录")
    @PostMapping("/login")
    public ResultVo login( HttpServletRequest request,@RequestBody Admin admin) {
        ResultVo resultVo=new ResultVo();
        if(adminService.loginAdmin(admin.getAdminAccountNumber(),admin.getAdminPassword())==null){
            resultVo=null;
        }else{
            admin=adminDao.queryByUsername(admin.getAdminAccountNumber());
            resultVo=new ResultVo<>(admin);
            request.getSession().setAttribute("adminAccountNumber",admin.getAdminAccountNumber());
            request.getSession().setAttribute("adminName",admin.getAdminName());
            request.getSession().setMaxInactiveInterval(60*60);
        }
        // Cookie cookie=new Cookie("username",admin);
        // response.setHeader("Set-Cookie","JSESSIONID=xxx;SameSite=None;Secure");
        // cookie.setMaxAge(24*60*60);       //存活一天
        //response.addCookie(cookie);
        return resultVo;
    }
    @GetMapping("/showSession")
    public String showSession() {
        String mag=(String)request.getSession().getAttribute("adminName");
        return mag;
    }
    @MyLog(value = "通过日志id查询日志")
    @GetMapping("/queryLogById")
    public ResultVo listLogById(LogQueryDto logQueryDto,int id) throws Exception{
        //调用业务
        logQueryDto.setId(id);
        PageUtil<SysLog> data=sysLogService.pageList(logQueryDto);
        //响应
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @GetMapping("/queryLog")
    public ResultVo listLog(LogQueryDto logQueryDto) throws Exception{
        //调用业务
        PageUtil<SysLog> data=sysLogService.pageList(logQueryDto);
        //响应
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @MyLog(value = "通过用户名查询日志")
    @GetMapping("/queryLogByUser")
    public ResultVo listLogByUsername(LogQueryDto logQueryDto,String username) throws Exception{
        //调用业务
        LogQueryDto logQueryDto1=new LogQueryDto();
        logQueryDto1.setUsername(username);
        PageUtil<SysLog> data=sysLogService.pageList(logQueryDto);
        //响应
        ResultVo resultVo=new ResultVo<>(data);
        return resultVo;
    }
    @MyLog(value="管理员删除日志")
    @PostMapping("/deleteLog")
    public String deleteLog(Integer id){
        sysLogDao.deleteLog(id);
        return null;
    }
    @MyLog(value="管理员注销")
    @GetMapping("/logout")
    public String logout(String username) {
        //清除session
        request.getSession().invalidate();
        return "redirect:/login";
    }
    @MyLog(value="审核")
    @PostMapping("/auditProjectSuccess")//等一个dao接口
    public ResultVo toExamine(String auditOpinion,Integer projectNo) throws Exception {
        ApplyQueryDto applyQueryDto=new ApplyQueryDto();
        applyQueryDto.setProjectNo(projectNo);
        auditProjectLibraryService.pageList(applyQueryDto);
        Apply apply=new Apply();
        apply.setProjectNo(projectNo);
        apply.setAuditOpinion(auditOpinion);
        apply.setAuditStatus("2");
        apply.setReviewer((String)request.getSession().getAttribute("adminName"));
        Date date =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String applyDate=sdf.format(date);
        apply.setAuditTime(applyDate.substring(0,10));
        applyDao.updateApply(apply);
        Apply project=applyDao.queryByNo(projectNo);
        if(applyDao.queryInvestment(projectNo)>=100000000.00){
            FiveYearProject fiveYearProject=new FiveYearProject();
            fiveYearProject.setProjectNo(project.getProjectNo());
            fiveYearProject.setFiveProjectType(project.getProjectType());
            fiveYearProject.setFiveSpecificType(project.getSpecificType());
            fiveYearProject.setFiveInvestment(project.getInvestment());
            fiveYearProject.setFiveRevenue(project.getRevenue());
            fiveYearProject.setFiveStartTime(project.getStartTime());
            fiveYearProject.setFiveLifeCycle(project.getLifeCycle());
            fiveYearProject.setFiveProjectAddress(project.getProjectAddress());
            fiveYearProject.setFiveProjectAssignmentPaper(project.getProjectAssignmentPaper());
            fiveYearProject.setFiveRevenueAnalysis(project.getRevenueAnalysis());
            fiveYearProject.setFiveConstructionStatus("未施工");
            fiveYearProject.setFiveReviewer((String)request.getSession().getAttribute("adminName"));
            fiveYearProject.setFiveName(project.getApplyName());
            fiveYearProject.setEnterpriseId(project.getEnterpriseId());
            fiveYearProject.setFiveAuditOpinion(project.getAuditOpinion());
            fiveYearProject.setFiveAuditTime(applyDate.substring(0,10));
            fiveProjectDao.addFive(fiveYearProject);
        }else{
            ThreeYearProject threeYearProject=new ThreeYearProject();
            threeYearProject.setProjectNo(project.getProjectNo());
            threeYearProject.setThreeProjectType(project.getProjectType());
            threeYearProject.setThreeSpecificType(project.getSpecificType());
            threeYearProject.setThreeInvestment(project.getInvestment());
            threeYearProject.setThreeRevenue(project.getRevenue());
            threeYearProject.setThreeStartTime(project.getStartTime());
            threeYearProject.setThreeLifeCycle(project.getLifeCycle());
            threeYearProject.setThreeProjectAddress(project.getProjectAddress());
            threeYearProject.setThreeProjectAssignmentPaper(project.getProjectAssignmentPaper());
            threeYearProject.setThreeRevenueAnalysis(project.getRevenueAnalysis());
            threeYearProject.setThreeConstructionStatus("未施工");
            threeYearProject.setThreeReviewer((String)request.getSession().getAttribute("adminName"));
            threeYearProject.setThreeName(project.getApplyName());
            threeYearProject.setEnterpriseId(project.getEnterpriseId());
            threeYearProject.setThreeAuditOpinion(project.getAuditOpinion());
            threeYearProject.setThreeAuditTime(applyDate.substring(0,10));
            threeProjectDao.addThree(threeYearProject);
        }
        Apply mag =apply;
        ResultVo resultVo=new ResultVo<>(mag);
        applyDao.deleteProject(projectNo);
        return resultVo;
    }
    @PostMapping("/toExamineFail")
    public ResultVo toExamineFail(String auditOpinion,Integer projectNo) throws Exception {
        ApplyQueryDto applyQueryDto=new ApplyQueryDto();
        applyQueryDto.setProjectNo(projectNo);
        auditProjectLibraryService.pageList(applyQueryDto);
        Apply apply=new Apply();
        Date date =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        apply.setAuditTime(sdf.format(date));
        apply.setProjectNo(projectNo);
        apply.setAuditOpinion(auditOpinion);
        apply.setAuditStatus("1");
        apply.setReviewer((String)request.getSession().getAttribute("adminName"));
        applyDao.updateApply(apply);
        applyDao.deleteProject(projectNo);
        Apply mag =apply;
        ResultVo resultVo=new ResultVo<>(date);
        return resultVo;
    }
    @GetMapping("/listDelete")
    public ResultVo listDelete(AdminQueryDto adminQueryDto) throws Exception {
        PageUtil<Admin> data =adminService.cleanAdmin(adminQueryDto);
        ResultVo resultVo=new ResultVo<>(data);
        return  resultVo;
    }
    @PostMapping("/reduction")
    public ResultVo reduction(int adminNo) throws Exception{
        Admin admin=new Admin();
        admin.setAdminNo(adminNo);
        admin.setAdminStateId(1);
        adminDao.updateAdmin(admin);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }
    @PostMapping("/cleanAdmin")
    public ResultVo cleanAdmin(int adminNo)throws Exception{
        adminDao.cleanAdmin(adminNo);
        ResultVo resultVo=new ResultVo();
        return resultVo;
    }




}
